import React, { useState, useEffect, useCallback } from 'react';
import moment from 'moment';
import { getLocalUserInfo } from 'utils/util';
import socket from './socket';
import produce from 'immer';
import { message } from 'antd';

export const HomeContext = React.createContext({});

// const types = ['private', 'group', 'all']
export default (props) => {
    const userInfo = getLocalUserInfo();
    const user = { ...userInfo, id: userInfo.id + '' };
    const [active, setActive] = useState('all')
    const [contact, setContact] = useState([
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '222222222222222222', // 好友的id
            username: 'hook', // 该账号名
            remark: '一只小鸭子', // 该账号昵称
            nickname: '王丽', // 我给这个好友的备注
        },
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '333333333333333333', // 好友的id
            username: '布为', // 该账号名
            remark: '布为', // 该账号昵称
            nickname: '狗子2号', // 我给这个好友的备注
        },
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '44444444444444444', // 好友的id
            username: '布为', // 该账号名
            remark: '布为', // 该账号昵称
            nickname: '狗子3号', // 我给这个好友的备注
        },
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '222222222222222222', // 好友的id
            username: 'hook', // 该账号名
            remark: '一只小鸭子', // 该账号昵称
            nickname: '王丽', // 我给这个好友的备注
        },
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '333333333333333333', // 好友的id
            username: '布为', // 该账号名
            remark: '布为', // 该账号昵称
            nickname: '狗子2号', // 我给这个好友的备注
        },
        {
            userId: '1111111111111111', // 我的用户id
            contactId: '44444444444444444', // 好友的id
            username: '布为', // 该账号名
            remark: '布为', // 该账号昵称
            nickname: '狗子3号', // 我给这个好友的备注
        },
    ])
    const [group, setGroup] = useState([

    ])
    const [conversation, setConversation] = useState({
        'all': {
            roomId: 'all',
            roomType: 'group',
            name: '全体成员',
            message: [],
            online: [],
        },
        'SDLFSLDFJSDFLSDFS123ASA': {
            roomId: 'SDLFSLDFJSDFLSDFS123ASA',
            roomType: 'private',
            name: '小岛',
            message: [],
            online: [],
        },
    })
    const createGroupRoom = function (r) {
        let type = '1';
        let d = r.reduce((total, current, index, arr) => {
            total.to += current.to + (index >= arr.length ? '' : ',')
            total.name += current.name + (index >= arr.length ? '' : ',')
            return total;
        }, {
            from: user.id,
            type
        })
        d.name.substr(12);
        socket.emit('createRoom', d)
    };
    const createPrivateRoom = function (r) {
        let type = '0';
        let { to, name } = r;
        let d = {
            from: user.uuid,
            to,
            name,
            type
        }
        socket.emit('createPrivateRoom', d)
    };
    const value = {
        user,
        active, setActive,
        contact, setContact,
        group, setGroup,
        conversation, setConversation,
        socket,
        createPrivateRoom,
        createGroupRoom,
    }

    const getHistoryMessage = function (chatId) {
        // conversation.map((conversation)=>{
        //     if(conversation.includes(chatId)&&conversation.message.length){
        //         return
        //     }
        // })
        // socket.emit('getHistoryMessage', {
        //     roomId: active,
        // })
    }
    useEffect(() => {
        if (!active) {
            return;
        }
        getHistoryMessage(active)
    }, [active])
    useEffect(() => {
        console.log('context执行次数')
        socket.emit('login', {
            userId: user.uuid,
            username: user.username,
            email: user.email,
        })
        socket.emit('contact/add', {
            userId: user.uuid,
            contactId: '3bf81fee-c3a9-4581-bdf5-c1fc7fdcf79d',
            remark: '布为',
        }, () => {
            console.log('添加好友成功')
        })

        socket.on('contactPrivate/list', (data) => {
            console.log('contactPrivate/list', data)
            setContact(data)
        })
        socket.on('contactGroup/list', (data) => {
            console.log('contactGroup/list', data)
            setGroup(data)
        })
        socket.on('conversation/list', (data) => {
            console.log('conversation/list', data)
            setConversation(data)
        })
        // socket.emit('contact/conversation')
        // socket.emit('group/conversation')
        // 好友列表
        // socket.on('contact/conversation', (data) => {
        //     console.log('contactList', data)
        // })
        // 群组列表
        // socket.on('group/conversation', (data) => {
        //     console.log('groupList', data)
        // })
        // socket.emit('createPrivateRoom', {
        //     from: 'd4eeffe71c8b4b848066417354b20900',
        //     to: '3bf81feec3a94581bdf5c1fc7fdcf79d',
        //     name: '不为',
        //     type: '0'
        // })

        // socket.on('online', (data) => {
        //     console.log('online', data)
        //     const nextState = produce(conversation, draft => {
        //         draft['all'].online = data;
        //     })
        //     setConversation(nextState)
        // })
        // 获取会话列表
        // socket.on('conversations', (data) => {
        //     console.log('conversations', data)
        //     const nextState = produce(conversation, draft => {
        //         draft = data.reduce((total, current, index, arr) => {
        //             if (total[current.roomId]) {
        //                 return total;
        //             }
        //             total[current.roomId] = current;
        //             return total;
        //         }, conversation)
        //         return draft;
        //     })
        //     setConversation(nextState)
        //     setActive('all')
        // })
        // socket.on('createRoom', (data) => {
        //     console.log('createRoom', data)
        //     const nextState = produce(conversation, draft => {
        //         draft[data.roomId] = {
        //             ...data,
        //             message: [],
        //             online: [],
        //         };
        //     })
        //     setConversation(nextState)
        //     setActive(data.roomId)
        // })
        // socket.on('receiveMsg', (data) => {
        //     console.log('receiveMsg', data)
        //     setConversation(state => {
        //         const nextState = produce(state, draft => {
        //             draft[data.roomId].message.push(data)
        //         })
        //         return nextState
        //     })
        // })
        // socket.on('getHistoryMessage', (data) => {
        //     console.log('getHistoryMessage', data)
        //     setConversation(state => {
        //         const nextState = produce(state, draft => {
        //             draft[data.roomId].message = data.message
        //         })
        //         return nextState
        //     })
        // })
        // socket.on('online', (data) => {
        //     console.log('online', data)
        //     setConversation(state => {
        //         const nextState = produce(state, draft => {
        //             Object.keys(state).map(key => {
        //                 draft[key] = state[key];
        //                 if (['1', '2'].includes(draft[key].type)) {
        //                     draft[key].online.push(data.uuid)
        //                     draft[key].count += 1
        //                 }
        //             })
        //         })
        //         return nextState
        //     })
        // })
        // socket.on('offline', (data) => {
        //     setConversation(state => {
        //         const nextState = produce(state, draft => {
        //             Object.keys(state).map(key => {
        //                 draft[key] = state[key];
        //                 if (['1', '2'].includes(draft[key].type)) {
        //                     let online = draft[key].online.filter(item => {
        //                         return item != data.uuid;
        //                     })
        //                     draft[key].online = online;
        //                     draft[key].count -= 1
        //                 }
        //             })
        //         })
        //         return nextState
        //     })
        // })
        // socket.on("test", (data) => {
        //     console.log("test", data)
        // })
        // socket.on("disconnect", (reason) => {
        //     console.log("disconnect", reason)
        // })
    }, [])

    return value;
}